package com.goatstone.client;

import org.gwt.mosaic.ui.client.WindowPanel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

public class MainWidgetComposite extends Composite {

	// set through parameters
	public final ServerServiceAsync serverService;
	private User user; 

	// widgets
	final AbsolutePanel mainPanel = new AbsolutePanel();
	final HorizontalPanel controls = new HorizontalPanel();
	final SimplePanel feedBack = new SimplePanel();			

	final VerticalPanel title = new VerticalPanel();			
	final TextBox textInput = new TextBox();
	private ColorPicker colorPicker;
	private FontSizePicker fontSizePicker;
	public AlphabCanvas alphabCanvas;
	public AlphaCanvasEventHandler alphaCanvasEventHandler;
	Label label = new Label();

	public MainWidgetComposite(User alphaDrawUser, ServerServiceAsync serverService){

		label.setStyleName("label");

		this.user = alphaDrawUser;
		this.serverService =   serverService;

		// title widget
		title.add(new HTML( "<h1><a href=\"http://goatstone.com\" target=\"new\">Goatstone</a>:&nbsp;AphabDraw</h1>" )	);
		HTML about = new HTML("<p>About</p>");
		about.addStyleName("about");
		about.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent arg0) {
				setFeedBack("congrats!");
				createWindowPanel();
			}});		
		title.add( about );
		title.addStyleName("title");
			
		textInput.setStyleName("textBox");
		feedBack.addStyleName("feedback");
		feedBack.add(label);
		colorPicker =  new ColorPicker(this);
		fontSizePicker = new FontSizePicker(this);

		alphabCanvas = new AlphabCanvas( new AlphaCanvasEventHandler());
		alphabCanvas.setStyleName("alpha_canvas");

		controls.add(title);
		controls.addStyleName("controls");
		controls.add(textInput);
		controls.add(colorPicker);		
		controls.add(fontSizePicker);		

		mainPanel.setStyleName("main_panel");
		mainPanel.add(controls,0,0);
		mainPanel.add(alphabCanvas);		

		initWidget(mainPanel);

		DOM.setStyleAttribute(RootPanel.get("about").getElement(), "display", "none");

		RootPanel.get().add(this);

		RootPanel.get().add(feedBack);
		DOM.setStyleAttribute(feedBack.getElement(), "top", getFeedBackTop()+"px");

		textInput.setFocus(true);

		// place the feedBack panel at the bottom of the page
		Window.addResizeHandler(new ResizeHandler(){
			@Override
			public void onResize(ResizeEvent arg0) {
				DOM.setStyleAttribute(feedBack.getElement(), "top", getFeedBackTop()+"px");
			}
		})	;

		//		setFeedBack("Welcome to AlphabDraw. Type into the text box above and click the mouse onto the canvas area.");
//		createWindowPanel();
	}	

	private void createWindowPanel() {
		String aboutText = RootPanel.get("about").getElement().getInnerHTML();
		setFeedBack(":"+aboutText);

		WindowPanel w = new WindowPanel();
		w.setPopupPosition(50,80 );
		HTML content =  new HTML(aboutText);
		content.setStyleName("aboutContent");
		w.setWidget( content ) ;
		w.setPixelSize(800, 480);//		w.pack();	    //w.center();
		w.show();
	}

	public void setFeedBack(String message){
		label.setText(message);
	}
	private int getFeedBackTop(){		
		return  Window.getClientHeight() - feedBack.getElement().getClientHeight() ;
	}
	public void setTextBoxFocus(){
		textInput.setFocus(true);		
	}
	public void setTextBoxFontColor(int[] color){
		DOM.setStyleAttribute(textInput.getElement(), "color", "rgb("+color[0]+","+color[1]+","+color[2]+" )");
	}
	public int getFontSize(){
		return fontSizePicker.getSelectedFontSize();
	}
	public int[] getFontColor(){
		return colorPicker.getSelectedColor();
	}
	public String getTextInput(){
		return textInput.getText();
	}
	class AlphaCanvasEventHandler implements ClickHandler{
		private Widget eventSrc;
		private NativeEvent nativeEvnt;

		public void onClick(ClickEvent event) {
			this.eventSrc=(Widget)event.getSource();
			this.nativeEvnt=event.getNativeEvent();

			DrawElement drawElement = new DrawElement();
			drawElement.text =  getTextInput();
			drawElement.x = getPointerX();
			drawElement.y = getPointerY();
			drawElement.fontSize =  getFontSize();
			drawElement.color = getFontColor();

			// use injector on this ??????????????
			if( user.getId() != null){
				drawElement.userId =  user.getId();
			}

			alphabCanvas.addDrawElement(drawElement);

			ClientRequestData clientRequestData = new ClientRequestData();
			clientRequestData.drawElement = drawElement;
			clientRequestData.userId =  user.getId();

			serverService.addElement( clientRequestData, new AsyncCallback<AddElementServerResponseData>() {
				public void onFailure(Throwable caught) {
					GWT.log("fail addElement", null);
				}
				public void onSuccess(AddElementServerResponseData addElementServerResponseData) {
				}
			});
			textInput.setFocus(true);
		}
		int getPointerX(){
			return nativeEvnt.getClientX() - eventSrc.getAbsoluteLeft();
		}
		int getPointerY(){
			return nativeEvnt.getClientY() - eventSrc.getAbsoluteTop();
		}
	}	
}
